$(function () {
    layui.use(['element','util'], function(){
        var element = layui.element,
            util = layui.util;

        // let closeSession = locals.get('isClose');
        // let isClose =  closeSession == null ? true : closeSession;
        // if(isClose){
        //     $('.layui-side').addClass('close');
        //     locals.set('isClose', isClose);
        // }else{
        //     $('.layui-side').removeClass('close');
        //     locals.set('isClose', isClose);
        // }

        // 获取产线信息，插入到侧边导航设备管理下级
        lineInfo();
        function lineInfo(){
            $.ajax({
                type:"post",
                url: ctx+"/line/getAll.do",
                data:{},
                async:	true,
                cache: false,
                dataType:"json",
                success:function(res){
                    if(res.code=="000000"){
                        let data = res.data;
                        // let data = [{
                        //         id:1,name:'des线A线'
                        //     },{
                        //         id:2,name:'des线B线'
                        //     }];
                        let html = '';
                        for (let i = 0; i < data.length; i++) {
                            html += '<dd><a href="${ctx}/deviceManage/deviceManage.do" lineId="' + data[i].id +'">' + data[i].name + '</a></dd>'
                        }
                        $('.line-info').prepend(html);
                        element.render();
                        // locals.set('allLines', data);
                    }else{
                        console.error(res.message);
                    }
                }
            });
        }

        // 点击设备管理下面的产线名，记录产线id、产线名
        $('body').on('click','.layui-nav .line-info a:not(.dev-set)',function(){
            let line = {
                lineId:  $(this).attr('lineId'),
                lineName:  $(this).text(),
            };
            // locals.set('lineInfo', line);
        });

        // 导航小三角 icon
        // $('.layui-header .layui-nav-item').find('.layui-nav-more').html('<i class="layui-icon layui-icon-down"></i>');
        $('.layui-header .layui-nav-item').find('.layui-nav-more').attr('class','triangle layui-icon layui-icon-down');
        var lis = $('.layui-side .layui-nav-item');
        for (var i = 0; i < lis.length; i++) {
            var el = $(lis[i]);
            if(el.hasClass('layui-nav-itemed')){
                el.find('.layui-nav-more').html('<i class="layui-icon layui-icon-up"></i>');
                var dd = el.children('dd');
                if(dd.length > 0){
                    if(dd.hasClass('layui-nav-itemed')){
                        dd.find('.layui-nav-more').html('<i class="layui-icon layui-icon-up"></i>');
                    }else{
                        dd.find('.layui-nav-more').html('<i class="layui-icon layui-icon-down"></i>');
                    }
                }
            }else{
                el.find('.layui-nav-more').html('<i class="layui-icon layui-icon-down"></i>');
            }
        }
        $('.layui-side .layui-nav-item').on('click',function (event) {
            if($(this).hasClass('layui-nav-itemed')){
                $(this).children('a').find('.layui-nav-more').html('<i class="layui-icon layui-icon-up"></i>');
            }else{
                $(this).children('a').find('.layui-nav-more').html('<i class="layui-icon layui-icon-down"></i>');
            }
        });
        // 侧边导航 - 三级
        $("body").on("click", '.menu_three',function () {
            var dd = $(this).parent();
            if(dd.hasClass('layui-nav-itemed')){
                $(this).find('.layui-nav-more').html('<i class="layui-icon layui-icon-up"></i>');
                dd.siblings().find('.layui-nav-more').html('<i class="layui-icon layui-icon-down"></i>');
                $(this).next().css('display','block');
                dd.siblings().find("ol").css('display','none');
            }else{
                $(this).find('.layui-nav-more').html('<i class="layui-icon layui-icon-down"></i>');
                $(this).next().css('display','none');
            }
        });
        $("ol").on("click", "li a", function () {
            $.each($(this).parent().siblings(), function (i, e) {
                $(e).find("a").removeClass('three_this')
            });
            $(this).addClass('active'); // 添加当前元素的样式
        });

        var pathname = window.location.pathname;
        if(pathname == '/'){ // 主页重定向
            window.location.href = '/integrate/integrateIndex.do'
        }
        // 设置网页title
        let titleText = $(".layui-side li a[href='"+ pathname +"']").text();
        let pageTitle = '<title>' + titleText + '</title>';
        $('head').prepend(pageTitle);

        // 退出登录
        $("body").on("click","#logout",function(){
            $.ajax({
                type:"post",
                url: ctx+"/logout.do",
                data:{},
                async:	true,
                cache: false,
                dataType:"json",
                beforeSend:function(){
                    //layer.load(2);
                },success:function(data){
                    if(data.code=="000000"){
                        window.localStorage.clear();
                        location.reload();
                    }else{
                        alert("系统异常，"+data.message);
                    }
                },error:function(e){
                    alert("系统异常，请稍后再试");
                }
            });
        });

        // 系统时间
        setInterval(function(){
            $('.sys-time span').text(util.toDateString(new Date()));
            let hour = new Date().getHours();  //  0 ~ 23
            // 早8~晚8 为白班，其余夜班
            if(hour >= 8 && hour <= 20 ){
                $('.shift-icon > .day-shift').show();
                $('.shift-icon > .night-shift').hide();
            }else{
                $('.shift-icon > .day-shift').hide();
                $('.shift-icon > .night-shift').show();
            }
        },1000);


        $('#triangle i').click(function(){
            $('.layui-side').toggleClass('close');
            let isClose = $('.layui-side').hasClass('close');
            // locals.set('isClose', isClose);
            if(!isClose){ // 侧边栏展开时，给当前nav加active样式
                let leftNav = $(".layui-side li a[href='"+ pathname +"']");
                leftNav.addClass("active");
                leftNav.parents('li').children('a').addClass("active");
                leftNav.parents('li.layui-nav-item').addClass("layui-nav-itemed");
            }
        });

    });
});